home *** CD-ROM | disk | FTP | other *** search
/ NetNews Offline 2 / NetNews Offline Volume 2.iso / news / comp / lang / c++-part2 / 15694 < prev    next >
Encoding:
Text File  |  1996-08-05  |  2.4 KB  |  93 lines

  1. Path: news.compuserve.com!newsmaster
  2. From: Philippe Verdy <100105.3120@compuserve.com>
  3. Newsgroups: comp.lang.c++
  4. Subject: Re: Exceptions *NOT* being handled properly?!?
  5. Date: 7 Apr 1996 21:23:33 GMT
  6. Organization: CompuServe Incorporated
  7. Message-ID: <4k9bol$fl9@arl-news-svc-5.compuserve.com>
  8. NNTP-Posting-Host: ad04-110.compuserve.com
  9.  
  10. mat@dekard.demon.co.uk (Matt at Home) s'Θcrit :
  11. > Compiler: gnu g++ v2.7.2 (i386-univel-sysv4.2MP)
  12. > Environment: UnixWare 2.03 Application Server (16Mb Ram,486DX2-80)
  13. > Problem: Exceptions do not seem to be handled correctly (according to
  14. > FAQ 261 of Cline & Lomow)
  15. > Given the following code
  16. >     class X {};
  17. >     class X2 : public X {};    // X2 is derived from X
  18. >     ....
  19. >     try
  20. >     {
  21. >         throw X2();    // throw the derived class
  22. >     }
  23. >     catch( const X2& e )    // catch the derived class
  24. >     {
  25. >         cerr << "Caught exception: Type X2" << endl;
  26. >     }
  27. >     catch( const X& e )    // catch the base class
  28. >     {
  29. >         cerr << "Caught exception: Type X" << endl;
  30. >     }
  31. >     catch( ... )        // catch anything left over
  32. >     {
  33. >         cerr << "Caught exception: Unknown type" << endl;
  34. >     };
  35. >     ....
  36. > Running this code correctly gives the output
  37. >     "Caught exception: Type X2"
  38. > However if we modify to remove the catch-block for X2 giving :-
  39. >     try
  40. >     {
  41. >         throw X2();    // throw the derived class
  42. >     }
  43. >     catch( const X& e )    // catch the base class
  44. >     {
  45. >         cerr << "Caught exception: Type X" << endl;
  46. >     }
  47. >     catch( ... )        // catch anything left over
  48. >     {
  49. >         cerr << "Caught exception: Unknown type" << endl;
  50. >     };
  51. >     ....
  52. > According to common sense AND to Cline & Lomow FAQ261, I expected :-
  53. >     "Caught exception: Type X"
  54. > Because X2 is a class derived from X ( i.e. we are catching an
  55. > exception through the base class, Cline & Lomows xmsg)
  56. > But instead I get :-
  57. >     "Caught exception: Unknown type"
  58. > Can someone tell me :-
  59. > 1. What the correct behaviour is?
  60. > 2. Is there something wrong with the code I have written (I have
  61. > similar but *more functional* code which failes the same way in a
  62. > project)?
  63. > 3. Is there something wrong with the g++ compiler?
  64. > 4. If there is, is it likely to be fixed?
  65. > Regards,
  66. > "Confused"
  67. > ----
  68. > Matt Mower - Information Service Team
  69. > M19d.  The University of North London.
  70.  
  71. It is clearly a bug in your compiler.
  72. I tried your example with BC5.0 and it works as
  73. expected... showing that your X catcher can/must/should
  74. catch an X2 exception !!
  75.  
  76.